<?php
namespace App\Service\Admin;

class RoleService extends BaseService
{
    //数据列表
    public static function index($pas)
    {
        $res = self::dbConn(self::$dbStRole)
            ->where('del', 0)
            // ->where('id', '>', $role_id)
            ->get();
        if (!count($res)) {
            return self::toback(-2, '数据不存在');
        }
        $arys = [];
        foreach ($res as $v) {
            if ($v->id > 2) {
                $authOnes = self::dbConn(self::$dbStAuth)
                    ->where('del', 0)
                    ->where('role_id', $v->id)
                    ->get();
                $authstr = '';
                if (count($authOnes)) {
                    foreach ($authOnes as $vv) {
                        $actOne = self::dbConn(self::$dbStAction)
                            ->where('pid', '>', 0)
                            ->where('id', $vv->action_id)
                            ->first();
                        if ($actOne) {
                            $authstr .= $authstr ? '，'.$actOne->title : $actOne->title;
                        }
                    }
                }
            } else if ($v->id == 2) {
                $authstr = '查看权限';
            } else {
                $authstr = '所有权限';
            }
            // if ($pas['is_leader'] && $v->id == 2) {
            //     $authstr .= '，任务发布';
            // }
            $arys[] = [
                'id' => $v->id, 'title' => $v->title,
                'authstr' => $authstr,
            ];
        }
        return self::toback(0, '', $arys, count($arys));
    }

    //新增
    public static function add($title)
    {
        $res = self::dbConn(self::$dbStRole)
            ->where('title', $title)
            ->first();
        if (!empty($res)) {
            return self::toback(-2, '存在同名数据');
        }
        $res = self::dbConn(self::$dbStRole)->insertGetId([
            'title' => $title, 'addtime' => toDate(), 'uptime' => toDate(),
        ]);
        if (!$res) {
            return self::toback(-1, 'error');
        }
        return self::toback();
    }

    //修改
    public static function modify($id, $title)
    {
        $res = self::dbConn(self::$dbStRole)->where('id', $id)->first();
        if (empty($res)) {
            return self::toback(-2, '数据不存在');
        }
        $res = self::dbConn(self::$dbStRole)
            ->where('title', $title)
            ->where('id', '<>', $id)
            ->first();
        if (!empty($res)) {
            return self::toback(-2, '存在同名数据');
        }
        $res = self::dbConn(self::$dbStRole)
            ->where('id', $id)
            ->update(['title' => $title, 'uptime' => toDate()]);
        if (!$res) {
            return self::toback(-1, 'error');
        }
        return self::toback();
    }

    //删除
    public static function del($id)
    {
        $res = self::dbConn(self::$dbStRole)->where('id', $id)->first();
        if (empty($res)) {
            return self::toback(-2, '数据不存在');
        }
        $res = self::dbConn(self::$dbStRole)
            ->where('id', $id)
            ->update(['del' => 1, 'deltime' => toDate()]);
        if (!$res) {
            return self::toback(-1, 'error');
        }
        return self::toback();
    }

    //模块列表
    public static function actList($role_id)
    {
        $pOnes = self::dbConn(self::$dbStAction)
            ->where('pid', 0)
            ->get();
        if (!count($pOnes)) {
            return self::toback(-2, '没有数据');
        }
        $arys = [];
        foreach ($pOnes as $v) {
            $childs = self::dbConn(self::$dbStAction)
                ->where('pid', $v->id)
                ->orderBy('sort', 'desc')
                ->get();
            $subs = [];
            if (count($childs)) {
                foreach ($childs as $vv) {
                    $subAuth = self::dbConn(self::$dbStAuth)
                        ->where('del', 0)
                        ->where('role_id', $role_id)
                        ->where('action_id', $vv->id)
                        ->first();
                    $subs[] = [
                        'id' => $vv->id, 'title' => $vv->title,
                        'is_check' => $subAuth ? true : false,
                    ];
                }
            }
            $pAuth = self::dbConn(self::$dbStAuth)
                ->where('del', 0)
                ->where('role_id', $role_id)
                ->where('action_id', $v->id)
                ->first();
            $arys[] = [
                'id' => $v->id, 'title' => $v->title,
                'childs' => $subs,
                'is_check' => $pAuth ? true : false,
            ];
        }
        return self::toback(0, '', $arys);
    }

    //设置权限
    public static function setAuth($role_id, $act_id)
    {
        $res = self::dbConn(self::$dbStAuth)
            ->where('del', 0)
            ->where('role_id', $role_id)
            ->where('action_id', $act_id)
            ->first();
        if (empty($res)) {
            self::dbConn(self::$dbStAuth)->insertGetId([
                'role_id' => $role_id, 'action_id' => $act_id, 'addtime' => toDate(),
            ]);
        } else {
            self::dbConn(self::$dbStAuth)
                ->where('del', 0)
                ->where('role_id', $role_id)
                ->where('action_id', $act_id)
                ->update(['del' => 1, 'deltime' => toDate()]);
        }
        return self::toback();
    }
}